﻿//----------------------------------------------------------------------------
//  Copyright (C) 2004-2013 by EMGU. All rights reserved.       
//----------------------------------------------------------------------------

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using System.Drawing;
using helloworld2;

namespace HelloWorld2
{
    class Program2
    {
        static void Main(string[] args)
        {
            //Application.Run(new GUI());
            Image<Bgr, Byte> img1 = new Image<Bgr, Byte>("C:/Users/Simonsk/Documents/Visual Studio 2012/Projects/helloworld2/helloworld2/danmark3tif.tif");



            City start;
            City finish;
            int[] firstRed = new int[2];
            bool drawOrange = true;
            int cityCounter = 0;
            string[] cities = { "Skagen", "Aalborg", "Aarhus", "Koebenhavn", "Esbjerg", "Odense", "Aabenraa" };
            CityContainer ct = CityContainer.Instance;



            // ITERATE THROUGH ALL PIXELS!!!
            for (int y = 0; y < img1.Height; y++) //Y-scale
            {
                for (int x = 0; x < img1.Width; x++) //X-scale
                {
                    /*if (img1[o, i].Blue == 0 && img1[o, i].Green == 242 && img1[o, i].Red == 255) PAINT ROAD BLUE!!
                    {
                        img1[o, i] = new Bgr(255, 0, 0);
                    }*/

                    // LOCATE CITIES BEGIN----------------------------------------------------------------------------------------
                    if (img1[y, x].Blue == 36 && img1[y, x].Green == 28 && img1[y, x].Red == 237 && drawOrange == true)
                    {
                        firstRed[0] = y;
                        firstRed[1] = x;

                        City c = new City(cities[cityCounter], y + 3, x + 3); //+3 for the center of red square
                        cityCounter++;

                        Console.WriteLine("yx [" + (y + 3) + " " + (x + 3) + "] city: [" + c.getName() + " " + c.getCoordinateY() + " " + c.getCoordinateX() + " ]");

                        img1[y + 3, x + 3] = new Bgr(0, 128, 255); //Make orange dot in center of city.

                        drawOrange = false; //Don't draw again as long as we're in the same red square
                    }
                    else if (y > firstRed[0] + 7 && (x > firstRed[1] + 7 || x < firstRed[1])) //If more than 7x7 away from first red pixel, get rdy to draw orange again
                    {
                        drawOrange = true;
                    }
                    // LOCATE CITIES END-----------------------------------------------------------------------------------------

                }


            }



            Console.WriteLine("From where do you wish to travel?");
            string startCityName = Console.ReadLine();


            start = ct.findCity(startCityName);
            Console.WriteLine(start.getName() + " " + start.getCoordinateY() + " " + start.getCoordinateX());
            Console.ReadLine();

            ImageViewer.Show(img1, "Test Window");


            //Image<Gray, float> img2 = img1.Convert<Gray, float>(); convert to grayscale
            //Matrix<float> src = new Matrix<float>(img2.Width, img2.Height); Create matrix
            //CvInvoke.cvConvert(src, img2);

            // Console.WriteLine(src[1,1]);
            // Console.WriteLine(img1[10, 10]);

            // Console.ReadLine();




        }
    }
}

/* IDEAS: 
 * Need "seekToWards" variables, up, right eg.
 * Indicate crossways on picture in order to apply pathfinding algorithm.
 * Crossways might also be detectable by checking the amount of nearby yellow pixels.
 * Calculate nodelengths by a^2+b^2=c^2
 * IF PIXEL == RED YELLOW OR ORANGE --- PIXEL IS WALKABLE
 */